package com.sjgl.esdn.controller;

import com.sjgl.esdn.domain.AjaxResultESDN;
import com.sjgl.esdn.domain.entity.uc.*;
import com.sjgl.esdn.service.DeptService;
import com.sjgl.esdn.service.UserCenterService;
import com.sjgl.esdn.utils.FileUtilsESDN;
import com.sjgl.esdn.utils.SecurityUtilsESDN;
import com.sjgl.esdn.utils.StringUtilsESDN;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: cjb
 * @Date: 2022-09-01 09:30
 **/
@RestController
@RequestMapping("/userCenter")
public class UserCenterController {
    @Autowired
    UserCenterService userCenterService;

    @Autowired
    DeptService deptService;
    @GetMapping(value = {"/checkPassword"})
    public AjaxResultESDN getUser(Long userId, String inputPassword)
    {
        AjaxResultESDN ajax = AjaxResultESDN.success();
        if (StringUtilsESDN.isNotNull(userId))
        {
            String password = userCenterService.selectUserPasswordById(userId);
            boolean isTrue;
            if (SecurityUtilsESDN.matchesPassword(inputPassword,password)) {
                isTrue = true;
            } else {
                isTrue = false;
            }
            ajax.put(AjaxResultESDN.DATA_TAG, isTrue);
        }
        return ajax;
    }
    @GetMapping(value = {"/getUserInfoById" })
    public AjaxResultESDN getUserInfoById(Long userId)
    {
        AjaxResultESDN ajax = AjaxResultESDN.success();
        if (StringUtilsESDN.isNotNull(userId))
        {
            UserInfo userInfo = userCenterService.selectUserInfoById(userId);
            System.out.println(userInfo);
            ajax.put(AjaxResultESDN.DATA_TAG, userInfo);
        }
        return ajax;
    }
    @PostMapping(value = {"/updateUserInfo" })
    public AjaxResultESDN updateUserInfo(@RequestBody UserInfo userInfo)
    {
        Long userId = userInfo.getUserId();
        AjaxResultESDN ajax = AjaxResultESDN.success();
        if (StringUtilsESDN.isNotNull(userId)) {
            userCenterService.updateUserInfo(userInfo);
            ajax.put(AjaxResultESDN.DATA_TAG, userCenterService.selectUserInfoById(userId));
            System.out.println(userInfo.getBirthday());
        }
        return ajax;
    }
    @GetMapping("/getMyMessage")
    public AjaxResultESDN getMyMessage(Long userId){
        AjaxResultESDN ajax = AjaxResultESDN.success();
        if (StringUtilsESDN.isNotNull(userId))
        {
            List<MyMessage> myMessages = userCenterService.getMyMessage(userId);
            ajax.put(AjaxResultESDN.DATA_TAG, myMessages);
        }
        return ajax;
    }
    @RequestMapping("/getRecentBrowse")
    public AjaxResultESDN getRecentBrowse(Long userId){
        AjaxResultESDN ajax = AjaxResultESDN.success();
        if (StringUtilsESDN.isNotNull(userId))
        {
            List<RecentBrowse> recentBrowses = userCenterService.getRecentBrowseFile(userId);
            ajax.put(AjaxResultESDN.DATA_TAG, recentBrowses);
        }
        return ajax;
    }

    /**
     * 浏览历史
     *
     * @param userId 用户id
     * @return {@code AjaxResultESDN}
     */
    @PostMapping("/getBrowseHistory")
    public AjaxResultESDN getBrowseHistory(Long userId){
        AjaxResultESDN ajax = AjaxResultESDN.success();
        if (StringUtilsESDN.isNotNull(userId))
        {
            List<BrowseHistory> browseHistories = userCenterService.getBrowseHistory(userId);
            ajax.put(AjaxResultESDN.DATA_TAG, browseHistories);
        }
        return ajax;
    }
    @RequestMapping("/getUploadFile")
    public AjaxResultESDN getUploadFile(Long userId){
        AjaxResultESDN ajax = AjaxResultESDN.success();
        if (StringUtilsESDN.isNotNull(userId))
        {
            List<UploadFile> uploadFiles  = userCenterService.getUploadFile(userId);
            ajax.put(AjaxResultESDN.DATA_TAG, uploadFiles);
        }
        return ajax;
    }

    @GetMapping("/searchMyUploadFile")
    public AjaxResultESDN searchMyUploadFile(Long userId, String inputString){
        AjaxResultESDN ajax = AjaxResultESDN.success();
        if(StringUtilsESDN.isNotNull(userId)){
            List<UploadFile> uploadFiles  = userCenterService.getUploadFile(userId);
            List<UploadFile> searchFiles = new ArrayList<>();

            for(UploadFile file:uploadFiles){
                if(file.getFileName().contains(inputString)){
                    searchFiles.add(file);
                }
            }
            ajax.put(AjaxResultESDN.DATA_TAG,searchFiles);
        }
        return ajax;
    }
    @PostMapping("/updateUploadFile")
    public AjaxResultESDN updateFile(@RequestBody com.sjgl.esdn.domain.entity.uc.UploadFile uploadFile) {
        AjaxResultESDN ajax = AjaxResultESDN.success();
        List<String> depts=uploadFile.getDepts();
        System.out.println(depts);
        List<Long> deptIdList = deptService.getIdListByDeptNameList(depts);
        userCenterService.updateUploadFile(uploadFile, deptIdList);
        ajax.put(AjaxResultESDN.DATA_TAG, uploadFile);
        return ajax;
    }

    @GetMapping("/getMyDownloadFile")
    public AjaxResultESDN getMyDownloadFile(Long userId){
        AjaxResultESDN ajax = AjaxResultESDN.success();
        if(StringUtilsESDN.isNotNull(userId)){
            List<MyDownloadFile> myDownLoadFiles = userCenterService.getMyDownLoadFile(userId);
            ajax.put(AjaxResultESDN.DATA_TAG, myDownLoadFiles);
        }
        return ajax;
    }
    @GetMapping("/searchMyDownloadFile")
    public AjaxResultESDN searchMyDownloadFile(Long userId, String inputString){
        AjaxResultESDN ajax = AjaxResultESDN.success();
        if(StringUtilsESDN.isNotNull(userId)){
            List<MyDownloadFile> myDownLoadFiles = userCenterService.getMyDownLoadFile(userId);
            List<MyDownloadFile> searchedFiles = new ArrayList<>();
            for (MyDownloadFile file:myDownLoadFiles){
                if(file.getFileName().contains(inputString)) {
                    searchedFiles.add(file);
                }
            }
            ajax.put(AjaxResultESDN.DATA_TAG, searchedFiles);
        }
        return ajax;
    }

    @GetMapping("/getMyBlog")
    public AjaxResultESDN getMyBlog(Long userId){
        AjaxResultESDN ajax = AjaxResultESDN.success();
        if(StringUtilsESDN.isNotNull(userId)){
            List<UCBlog> UCBlogs = userCenterService.getUCBlogs(userId);
            ajax.put(AjaxResultESDN.DATA_TAG, UCBlogs);
        }
        return ajax;
    }

    @PostMapping("/updateMyBlog")
    public AjaxResultESDN updateMyBlog(@RequestBody com.sjgl.esdn.domain.entity.uc.UCBlog ucBlog) {
        AjaxResultESDN ajax = AjaxResultESDN.success();
        List<String> depts=ucBlog.getDepts();
        System.out.println(depts);
        List<Long> deptIdList = deptService.getIdListByDeptNameList(depts);
        userCenterService.updateMyBlog(ucBlog, deptIdList);
        ajax.put(AjaxResultESDN.DATA_TAG, ucBlog);
        return ajax;
    }

    @PostMapping("/createMyColumn")
    public AjaxResultESDN createMyColumn(Long userId,String columnName,String columnIntro) {
        AjaxResultESDN ajax = AjaxResultESDN.success();
        userCenterService.createMyColumn(userId,columnName,columnIntro);
        return ajax;
    }
    @GetMapping("/getMyColumn")
    public AjaxResultESDN getMyColumn(Long userId){
        AjaxResultESDN ajax = AjaxResultESDN.success();
        if(StringUtilsESDN.isNotNull(userId)){
            List<MyColumn> myColumns = userCenterService.getMyColumn(userId);
            ajax.put(AjaxResultESDN.DATA_TAG, myColumns);
        }
        return ajax;
    }

    @GetMapping("/getMyColumnInfo")
    public AjaxResultESDN getMyColumnInfo(Long columnId){
        AjaxResultESDN ajax = AjaxResultESDN.success();
        if(StringUtilsESDN.isNotNull(columnId)){
            MyColumnInfo myColumnInfo = userCenterService.getMyColumnInfo(columnId);
            ajax.put(AjaxResultESDN.DATA_TAG, myColumnInfo);
        }
        return ajax;
    }
//    @PostMapping("/addBlogToOther")
//    public AjaxResultESDN createMyColumn(Long userId,String columnName,String columnIntro) {
//        AjaxResultESDN ajax = AjaxResultESDN.success();
//        userCenterService.createMyColumn(userId,columnName,columnIntro);
//        return ajax;
//    }

    @PostMapping("/uploadColumnImg")
    public AjaxResultESDN uploadColumnImg(Long columnId, @RequestParam("multipartFile") MultipartFile multipartFile){
        AjaxResultESDN ajax = AjaxResultESDN.success();
        if (StringUtilsESDN.isNotNull(columnId)){
            String fileName = columnId.toString() + ".jpg";
            String prePath = "D:\\ESDN\\";
            String filePath = prePath + userCenterService.getColumnImg(columnId);
            System.out.println(filePath);
            String img = filePath + fileName;
            java.io.File file1= new java.io.File(img);
            if (!file1.getParentFile().exists()) {
                file1.getParentFile().mkdirs();
            }
            String result = FileUtilsESDN.saveFile(multipartFile,filePath,fileName);
            ajax.put(AjaxResultESDN.DATA_TAG,result);
        }
        return ajax;
    }

    @PostMapping("/uploadAvatar")
    public AjaxResultESDN uploadAvatar(Long userId, @RequestParam("multipartFile") MultipartFile multipartFile){
        AjaxResultESDN ajax = AjaxResultESDN.success();
        if (StringUtilsESDN.isNotNull(userId)){
            String fileName = userId.toString() + ".jpg";
            String prePath = "D:\\ESDN\\";
            String filePath = prePath + userCenterService.getAvatar();
            System.out.println(filePath);
            String result = FileUtilsESDN.saveFile(multipartFile,filePath,fileName);
            ajax.put(AjaxResultESDN.DATA_TAG,result);
        }
        return ajax;
    }

}
